//
//  TabBar.swift
//  MagaCore
//
//  Created by MrMessy on 2023/4/29.
//

import SwiftUI

struct HTTabBar: View {
    @Binding var var_selectIndex: Int

    var body: some View {
        TabView(selection: $var_selectIndex) {
            NavigationView {
                HTHomeScreen()
            }
            .tabItem {
                Image(var_selectIndex == 0 ? "home_sel" : "home")
                Text("Home")
            }
            .navigationViewStyle(.stack)
            .navigationBarHidden(true).navigationBarTitle("").tag(0)

            NavigationView {
                HTBrowseView()
            }
            .tabItem {
                Image(var_selectIndex == 1 ? "browse_sel" : "browse")
                Text("Browse")
            }
            .navigationViewStyle(.stack)
            .navigationBarHidden(true).navigationBarTitle("").tag(1)

            NavigationView {
                HTLibraryView()
            }
            .tabItem {
                Image(var_selectIndex == 2 ? "library_sel" : "library")
                Text("My Library")
            }
            .navigationViewStyle(.stack)
            .navigationBarHidden(true).navigationBarTitle("").tag(2)

            NavigationView {
                HTPremiumScreen()
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Image(var_selectIndex == 3 ? "premium_sel" : "premium")
                Text("Premium")
            }
            .navigationBarHidden(true)
            .navigationBarTitle("")
            .tag(3)
        }
        .onReceive(NotificationCenter.default.publisher(for: HTConfig.STATIC_notification_changeTabbar), perform: { notifier in
            /** 监听要改变的下标 */
            if let userInfo = notifier.userInfo,
               let currentIndex = userInfo["currentIndex"] as? Int {
                self.var_selectIndex = currentIndex
            }
        })
        .accentColor(Color(hex: "#FF6C3C"))
    }
}

extension View {
    @ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
        if condition {
            transform(self)
        } else {
            self
        }
    }
}
